package project.dade.cn.bst.adapter.base;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import cn.bingoogolapple.swipeitemlayout.BGASwipeItemLayout;

import project.dade.cn.bst.R;
import project.dade.cn.bst.model.MyaddressDate;

/**
 * 作者:王浩 邮件:bingoogolapple@gmail.com
 * 创建时间:15/5/22 16:31
 * 描述:
 */
public class RecyclerViewAdapter extends BGARecyclerViewAdapter<MyaddressDate> {
    /**
     * 当前处于打开状态的item
     */
    private List<BGASwipeItemLayout> mOpenedSil = new ArrayList<>();

    public RecyclerViewAdapter(RecyclerView recyclerView) {
        super(recyclerView, R.layout.item_myaddress);
    }

    @Override
    public void setItemChildListener(BGAViewHolderHelper viewHolderHelper) {
        BGASwipeItemLayout swipeItemLayout = viewHolderHelper.getView(R.id.sil_item_bgaswipe_root);
        swipeItemLayout.setDelegate(new BGASwipeItemLayout.BGASwipeItemLayoutDelegate() {
            @Override
            public void onBGASwipeItemLayoutOpened(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
                mOpenedSil.add(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutClosed(BGASwipeItemLayout swipeItemLayout) {
                mOpenedSil.remove(swipeItemLayout);
            }

            @Override
            public void onBGASwipeItemLayoutStartOpen(BGASwipeItemLayout swipeItemLayout) {
                closeOpenedSwipeItemLayoutWithAnim();
            }
        });
        viewHolderHelper.setItemChildClickListener(R.id.tv_item_bgaswipe_delete);
        viewHolderHelper.setItemChildLongClickListener(R.id.tv_item_bgaswipe_delete);
    }

    @Override
    protected void fillData(BGAViewHolderHelper helper, int position, MyaddressDate myaddressDate) {
        if (myaddressDate.getStat().equals("1")) {
                helper.getView(R.id.tv_default).setVisibility(View.VISIBLE);
               helper.setText(R.id.tv_order_name, myaddressDate.getUsername());
               helper.setText(R.id.tv_order_phone, myaddressDate.getPhone());
                helper.setText(R.id.tv_address, myaddressDate.getAddress());
            } else {
               helper.setText(R.id.tv_order_name, myaddressDate.getUsername());
                helper.setText(R.id.tv_order_phone, myaddressDate.getPhone());
                helper.setText(R.id.tv_address, myaddressDate.getAddress());


            }


    }



    public void closeOpenedSwipeItemLayoutWithAnim() {
        for (BGASwipeItemLayout sil : mOpenedSil) {
            sil.closeWithAnim();
        }
        mOpenedSil.clear();
    }
}